package com.company.control;

import com.company.bean.UserInfoBean;
import com.company.model.UserModel;
import com.company.util.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置编码值
        request.setCharacterEncoding("utf-8");
       //1.获取表单数据
        UserInfoBean userInfoBean = MyUtils.getNewInstance(UserInfoBean.class);
        userInfoBean.setUsername(request.getParameter("username"));
        userInfoBean.setUserpsw(request.getParameter("userpsw"));
        userInfoBean.setNewuserpsw(request.getParameter("newuserpsw"));
        String querenPsw = request.getParameter("querenPsw");

        //2.获取表单数据，并把表单数据保存到实例类中

        //3.使用model层进行数据保存的业务逻辑

        //4.数据保存成功之后，跳转的保存结果显示页面
        if(MyUtils.getNewInstance(UserModel.class)
                .SelectUser(userInfoBean.getUsername(),userInfoBean.getUserpsw())) {
            //将页面提交的数据存到Session里，供前端jsp调用
            if(!userInfoBean.getUserpsw().equals(userInfoBean.getNewuserpsw())
                    &&userInfoBean.getNewuserpsw().equals(querenPsw)) {

                boolean flag = MyUtils.getNewInstance(UserModel.class)
                        .UpdatePsw(userInfoBean);
                if (flag) {
                    request.getRequestDispatcher("WEB-INF/views/changeTrue.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("WEB-INF/views/changeFalse.jsp").forward(request, response);
                }
            }
            else
            {
                request.getRequestDispatcher("WEB-INF/views/changeFalse.jsp").forward(request, response);
            }
        }
    }
}
